import BaseLifeCyclePainting from "../../standard/entities/Painting/BaseLifeCyclePainting";
import Acceleration2D from "../../standard/entities/Movement/Acceleration2D";
import TrackEffect from "../../standard/entities/Effect/TrackEffect";
import Movement2D from "../../standard/entities/Movement/Movement2D";
import Circular from "../../standard/entities/Shape/Circular";
import BaseAnimationPainting from "../../standard/entities/Painting/BaseAnimationPainting";

export default class GravityCircularPainting extends BaseLifeCyclePainting {

    constructor({startPoint, r = 30, color = '#fff', mass = 100}) {
        super();
        this.startPoint = startPoint;
        this.r = r;
        this.color = color;
        this.mass = mass;
        this.controller.setStatus(BaseLifeCyclePainting.RUNNING);
    }

    onCanvasLoaded(canvas) {
        this.gravityCircular = new Circular(this.r, this.startPoint);
        this.gravityCircular.mass = this.mass;
        this.gravityCircular.setMovement(new Movement2D({
                speed: 0
            }).applyAcceleration('gravity', new Acceleration2D(0.1,180))
        );
    }

    onPainterLoaded(painter) {
        painter.fillStyle = this.color;
        this.activeDrawnStep((painter, data) => {
            painter.drawInSession(() => {
                painter.beginPath();
                painter.arc(data.x,
                    data.y,
                    data.r, 0, 2 * Math.PI);
                painter.fill();
            });
        });
        let effect = new TrackEffect(this.drawnStep, 1, 0.4, 0.005);
        this.addLifeCycleEffect(BaseAnimationPainting.RUNNING, effect);
    }

    exportDrawnData() {
        return {
            x: this.gravityCircular.centre.x,
            y: this.gravityCircular.centre.y,
            r: this.r,
            color: this.color,
        };
    }

    nextFrameOnRunning(controller) {
        super.nextFrameOnRunning(controller);
        this.gravityCircular.moveByTrack(movement => {
            if (movement.speed2D.verticalSpeed > 0 && this.gravityCircular.centre.y + this.gravityCircular.r > this.canvas.height) {
                // 碰撞水平地面
                if (movement.speed2D.verticalSpeed >= -1 && movement.speed2D.verticalSpeed <= 1) {
                    // 重新赋予速度
                    movement.speed2D.verticalSpeed = -5;
                } else {
                    movement.angle = 180 - movement.angle + 360;
                    movement.angle %= 360;
                }
            }
            if (movement.speed2D.horizontalSpeed > 0 && this.gravityCircular.centre.x + this.gravityCircular.r > this.canvas.width) {
                // 碰撞右侧
                movement.angle = 360 - movement.angle;
                movement.angle %= 360;
            }
            if (movement.speed2D.horizontalSpeed < 0 && this.gravityCircular.centre.x - this.gravityCircular.r < 0) {
                // 碰撞左侧
                movement.angle = 360 - movement.angle;
                movement.angle %= 360;
            }

            // 检查小球间的碰撞
            let circles = this.canvas.paintings.map(painting => {
                if (painting instanceof GravityCircularPainting) {
                    return painting.gravityCircular;
                }
            }).filter(circle => {
                return circle;
            });
            this.gravityCircular.doCollisionChecked(circles);
        });
    }
}